package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.entity.AcceptanceMember;
import com.kaizeli.website.pojo.dto.AcceptanceMemberDTO;
import com.kaizeli.website.service.AcceptanceMemberService;
import com.kaizeli.website.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 验收成员控制器
 */
@RestController
@RequestMapping("/api/acceptance-member")
@CrossOrigin(origins = "*")
public class AcceptanceMemberController {

    @Autowired
    private AcceptanceMemberService acceptanceMemberService;

    /**
     * 根据验收单ID查询验收成员列表
     */
    @GetMapping("/acceptance/{acceptanceId}")
    public Result<List<AcceptanceMemberDTO>> getMembersByAcceptance(@PathVariable Long acceptanceId) {
        return acceptanceMemberService.getMembersByAcceptanceId(acceptanceId);
    }

    /**
     * 根据验收成员ID查询详情
     */
    @GetMapping("/{id}")
    public Result<AcceptanceMember> getMemberById(@PathVariable Long id) {
        return Result.success(acceptanceMemberService.getById(id));
    }

    /**
     * 创建验收成员
     */
    @PostMapping
    public Result<AcceptanceMember> createMember(@RequestBody AcceptanceMember member) {
        return acceptanceMemberService.createMember(member);
    }

    /**
     * 批量创建验收成员
     */
    @PostMapping("/batch")
    public Result<Void> batchCreateMembers(@RequestBody BatchCreateMembersRequest request) {
        return acceptanceMemberService.batchCreateMembers(request.getMembers());
    }

    /**
     * 更新验收成员
     */
    @PutMapping("/{id}")
    public Result<Void> updateMember(@PathVariable Long id, @RequestBody AcceptanceMember member) {
        return acceptanceMemberService.updateMember(id, member);
    }

    /**
     * 更新验收成员状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateMemberStatus(@PathVariable Long id, @RequestParam String status) {
        return acceptanceMemberService.updateMemberStatus(id, status);
    }

    /**
     * 删除验收成员
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteMember(@PathVariable Long id) {
        return acceptanceMemberService.deleteMember(id);
    }

    /**
     * 根据员工ID查询验收记录
     */
    @GetMapping("/employee/{employeeId}")
    public Result<List<AcceptanceMemberDTO>> getMembersByEmployee(@PathVariable Long employeeId) {
        return acceptanceMemberService.getMembersByEmployeeId(employeeId);
    }

    /**
     * 转项成员
     */
    @PutMapping("/{id}/transfer")
    public Result<Void> transferMember(@PathVariable Long id, @RequestBody TransferMemberRequest request) {
        return acceptanceMemberService.transferMember(id, request.getNewProjectCode(), request.getNewSubProjectId());
    }

    /**
     * 批量创建验收成员请求类
     */
    public static class BatchCreateMembersRequest {
        private List<AcceptanceMember> members;

        public List<AcceptanceMember> getMembers() {
            return members;
        }

        public void setMembers(List<AcceptanceMember> members) {
            this.members = members;
        }
    }

    /**
     * 转项请求类
     */
    public static class TransferMemberRequest {
        private String newProjectCode;
        private Long newSubProjectId;

        public String getNewProjectCode() {
            return newProjectCode;
        }

        public void setNewProjectCode(String newProjectCode) {
            this.newProjectCode = newProjectCode;
        }

        public Long getNewSubProjectId() {
            return newSubProjectId;
        }

        public void setNewSubProjectId(Long newSubProjectId) {
            this.newSubProjectId = newSubProjectId;
        }
    }
}
